{
  LW instancing -- LightWave Item Instancing.

  Based on lwinstancing.h which is:
  Copyright 2011, NewTek, Inc.
}

{
  Copyright 2012, Jameel Halabi

  This unit is part of the BlaiseWave wrapper,
  which is licensed under the BSD 2-Clause license (see licenses/license.txt).
}

unit LW_Instancing;

{$mode objfpc}{$H+}

interface

uses
  LW_Types   ,
  LW_Server  ,
  LW_Handler ,
  LW_Render  ;

const
  LWINSTANCER_HCLASS     = 'InstancerHandler'   ;
  LWINSTANCER_ICLASS     = 'InstancerInterface' ;
  LWINSTANCER_GCLASS     = 'InstancerGizmo'     ;
  LWINSTANCER_VERSION    = 1 ;

  LWITEMINSTANCERFUNCS_GLOBAL = 'ItemInstancerFuncs' ;
  LWITEMINSTANCEINFO_GLOBAL   = 'ItemInstanceInfo'   ;

  LWINSTEVAL_RENDER = 0 ;
  LWINSTEVAL_OPENGL = 1 ;

  // enum InstanceDrawerMode
  INSTDRAW_NONE     = 0 ;
  INSTDRAW_POINT    = 1 ;
  INSTDRAW_BBOX     = 2 ;
  INSTDRAW_MESH     = 3 ;

type
  PLWItemInstance = Pointer ;

  // The LWInstancerAccess struct.
  PLWInstancerAccess = ^TLWInstancerAccess;
  TLWInstancerAccess = record
    Instancer : PLWItemInstancer ;
    Mode      : Integer          ;
  end;

  // Instancer handler activation.
  PLWInstancerHandler = ^TLWInstancerHandler;
  TLWInstancerHandler = record
    Inst        : PLWInstanceFuncs;
    Item        : PLWItemFuncs    ;
    Rend        : PLWRenderFuncs  ;
    Evaluate    : procedure
                  (      Instance : PLWInstance        ;
                   const Access   : PLWInstancerAccess ) ;
  end;

  // Instancer funcs.
  PLWItemInstancerFuncs = ^TLWItemInstancerFuncs;
  TLWItemInstancerFuncs = record
    NumInstances    : function
                      (      Instancer : PLWItemInstancer ) : UInt32		  ;
    InstanceByIndex : function
                      (      Instancer : PLWItemInstancer ;
    		             Index     : UInt32		  ) : PLWItemInstance ;
    First           : function
                      (      Instancer : PLWItemInstancer ) : PLWItemInstance ;
    Next            : function
                      (      Instancer : PLWItemInstancer ;
    		             Instance  : PLWItemInstance  ) : PLWItemInstance ;
    CreateInstance  : function
                      (      Instancer : PLWItemInstancer ) : PLWItemInstance ;
    DestroyInstance : procedure
                      (      Instancer : PLWItemInstancer ;
    		             Instance  : PLWItemInstance  ) ;
    SetInstance     : procedure
                      (      Instancer : PLWItemInstancer ;
    		             ID        : UInt32           ;
    		             Item	   : PLWItem      ;
    			     Steps 	   : UInt32       ) ;
    SetMotionStep   : procedure
                      (      Instance  : PLWItemInstance  ;
    		             Steps     : UInt32           ;
    		       const Pos       : TLWDVector       ;
    		       const Scl       : TLWDVector       ;
    		       const Rot       : TLWDVector       ) ;
    SetMotions      : procedure
                      (      Instance  : PLWItemInstance  ;
    		             Steps     : UInt32           ;
    		       const Pos       : TLWDVector       ;
    		       const Scl       : TLWDVector       ;
    		       const Rot       : TLWDVector       ) ;
    SetMotionStepM  : procedure
                      (      Instance  : PLWItemInstance  ;
    		             Steps     : UInt32           ;
    		       const Pos       : TLWDVector       ;
    		       const Matrix    : Array {[0..8]} of Double  ) ;
    SetMotionsM     : procedure
                      (      Instance  : PLWItemInstance  ;
                             Steps     : UInt32           ;
                       const Pos       : TLWDVector       ;
                       const Matrix    : Array {[0..8]} of Double ) ;

    // GUI only
    SetInstanceDrawer : procedure
                        (      Instance  : PLWItemInstance  ;
    			       DrawMode  : Integer          ) ;  // Originally InstanceDrawerMode
    SetInstanceDrawerColor: procedure
                        (      Instance  : PLWItemInstance  ;
    			       Color     : UInt32           ) ; // Use RGB_(r, g, b) macro to make colors.
  end;

  // Instance information.
  PLWItemInstanceInfo = ^TLWItemInstanceInfo;
  TLWItemInstanceInfo = record
    Item     : function
               (      ItemInstID : PLWItemInstance ) : PLWItem  ;   // The LWItemID of the item being instanced.
    Steps    : function
               (      ItemInstID : PLWItemInstance ) : UInt32   ;   // The motion steps stored in the instance.
    Pos      : procedure
               (      ItemInstID : PLWItemInstance ;
                      Step       : UInt32          ;
                      P 	 : TLWDVector      ) ;              // The position of the instance.
    Scale    : procedure
               (      ItemInstID : PLWItemInstance ;
                      Step       : UInt32          ;
                      S          : TLWDVector      ) ;              // The scale of the instance.
    Rotation : procedure
               (      ItemInstID : PLWItemInstance ;
                      Step       : UInt32          ;
                      R 	 : TLWDVector      ) ;              // The rotation of the instance.
    ID       : function
               (      ItemInstID : PLWItemInstance ) : UInt32   ;   // The ID set by the generator.
    Matrix   : procedure
               (      ItemInstID : PLWItemInstance ;
                      Step       : UInt32          ;
                      M	         : array {[0..8]} of Double )   ;   // The rotation and scale matrix of the instance.
  end;

implementation

end.

